import statsTest
import hist
import numpy as np
import fit

def getLen(data,fun):
    num=0
    for i in data:
        if fun(i):
            num+=1
    return num

data = [141.0,148,132,138,154,142,150,146,155,158,150,140,147,148,144,150,149,145,149,158,143,141,144,144,126,140,
        144,142,141,140,145,135,147,146,141,136,140,146,142,137,-211,148,154,137,139,143,140,131,143,141,149,148,
        135,148,152,143,144,141,143,147,146,150,132,142,142,143,153,149,146,149,138,142,149,142,137,134,144,146,
        147,140,142,140,137,152,145]
r = statsTest.chis2_test(data)
print(r)

hist.draw_hist(data,'','width','num',(120,160),(0,30))

fi=[getLen(data,lambda x:x<135),getLen(data,lambda x:x>=135 and x<138),getLen(data,lambda x:x>=138 and x<142),
    getLen(data,lambda x:x>=142 and x<146),getLen(data,lambda x:x>=146 and x<150),getLen(data,lambda x:x>=150 and x<154),
    getLen(data,lambda x:x>=154)]

data=np.array(data)
mx=data.max()
mi=data.min()
mu=data.mean()
sigma=data.std()

fendian=[135,138,142,146,150,154]  # 区间的分点
pdf,cdf=fit.normFit(mu,sigma)
# 概率密度是分布函数的导数，分布函数是概率密度的积分上限函数
p0 = list(map(lambda x:cdf(x), fendian))  # 分点处分布函数的值
p1 = list(map(lambda x:pdf(x), fendian))  # 分布点处概率密度函数的值
p=[p0[0]]+p1+[1-p0[5]]

fi=np.array(fi)
p=np.array(p)
print(fi)
print(p)
'''
chi=np.power(fi-84*p,2)/(84*p)
print(p)
print(chi)
'''